<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html lang="zh-CN">
<head>
    <title>商品列表</title>
    <link href="${pageContext.request.contextPath}/static/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="bg-light">

<div class="container mt-4">
    <h2 class="mb-4">商品列表</h2>

    <div class="d-flex justify-content-between align-items-center mb-4">
        <a href="${pageContext.request.contextPath}/product/add" class="btn btn-success">新增商品</a>

        <form class="d-flex w-50" role="search" action="${pageContext.request.contextPath}/product/search" method="get">
            <input class="form-control me-2" type="search" name="keyword" placeholder="商品名称" aria-label="Search">
            <button class="btn btn-outline-success" type="submit">搜索</button>
        </form>
    </div>
    <!-- 商品表格 -->
    <div class="table-responsive">
        <table class="table table-striped table-hover align-middle">
            <thead class="table-dark">
            <tr>
                <th>ID</th>
                <th>名称</th>
                <th>类别</th>
                <th>品牌</th>
                <th>价格</th>
                <th>描述</th>
                <th>库存</th>
                <th>创建时间</th>
                <th>创建人</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            <c:choose>
                <c:when test="${not empty products and products.size() > 0}">
                    <c:forEach var="p" items="${products}">
                        <tr>
                            <td>${p.id}</td>
                            <td>${p.name}</td>
                            <td>${p.category}</td>
                            <td>${p.brand}</td>
                            <td>${p.price}</td>
                            <td>${p.description}</td>
                            <td>${p.stock}</td>
                            <td>${p.createTime}</td>
                            <td>${p.createBy}</td>
                            <td>
                                <a href="${pageContext.request.contextPath}/product/edit/${p.id}"
                                   class="btn btn-sm btn-primary me-1">编辑</a>

                                <form method="post"
                                      action="${pageContext.request.contextPath}/product/delete/${p.id}"
                                      style="display:inline;"
                                      onsubmit="return confirm('确定删除?')">
                                    <input type="hidden" name="_method" value="delete"/>
                                    <button type="submit" class="btn btn-sm btn-danger">删除</button>
                                </form>
                            </td>
                        </tr>
                    </c:forEach>
                </c:when>
                <c:otherwise>
                    <tr>
                        <td colspan="10" class="text-center text-muted py-4">
                            暂无商品数据，请点击上方 "新增商品" 添加数据。
                        </td>
                    </tr>
                </c:otherwise>
            </c:choose>
            </tbody>
        </table>
    </div>

    <!-- 分页导航 -->
    <nav aria-label="Page navigation">
        <ul class="pagination justify-content-center">
            <c:if test="${page > 1}">
                <li class="page-item">
                    <a class="page-link" href="?page=${page-1}&size=${size}">上一页</a>
                </li>
            </c:if>
            <li class="page-item disabled">
                <span class="page-link">第 ${page} 页</span>
            </li>
            <c:if test="${page * size < total}">
                <li class="page-item">
                    <a class="page-link" href="?page=${page+1}&size=${size}">下一页</a>
                </li>
            </c:if>
        </ul>
    </nav>
</div>

<!-- 引入 Bootstrap JS（可选） -->
<script src="${pageContext.request.contextPath}/static/js/bootstrap.bundle.js"></script>
</body>
</html>
